//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SwaptionVolatilityMatrix.h"
using namespace Cephei::QL::Termstructures::Volatility::Swaption;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Volatility/Swaption/SwaptionVolatilityDiscrete.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (DateTime referenceDate, Cephei::Core::IVector<DateTime>^ optionDates, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::QL::Math::IMatrix^ volatilities, Cephei::QL::Times::IDayCounter^ dayCounter) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
    CMatrix^ _Cvolatilities;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phSwaptionVolatilityMatrix = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        optionDates->Lock();
        INativeVector<DateTime>^ _NCIoptionDates = optionDates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCoptionDates = safe_cast<CDateTimeVector^>(_NCIoptionDates);
        std::vector<QuantLib::Date>& _optionDates = static_cast<std::vector<QuantLib::Date>&> (_NCoptionDates->GetReference ());
        swapTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIswapTenors = swapTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCswapTenors = safe_cast<CPeriodVector^>(_NCIswapTenors);
        std::vector<QuantLib::Period>& _swapTenors = static_cast<std::vector<QuantLib::Period>&> (_NCswapTenors->GetReference ());
        _Cvolatilities = safe_cast<CMatrix^> (volatilities);
        _Cvolatilities->Lock();
        QuantLib::Matrix& _volatilities = static_cast<QuantLib::Matrix&> (_Cvolatilities->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (new QuantLib::SwaptionVolatilityMatrix ( _referenceDate,  _optionDates,  _swapTenors,  _volatilities,  _dayCounter ));
        SetSwaptionVolatilityDiscrete (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (optionDates != nullptr) optionDates->Unlock();    //not optional
        if (swapTenors != nullptr) swapTenors->Unlock();    //not optional
        if (_Cvolatilities != nullptr) _Cvolatilities->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (DateTime referenceDate, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::QL::Math::IMatrix^ volatilities, Cephei::QL::Times::IDayCounter^ dayCounter) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
    CCalendar^ _Ccalendar;
    CMatrix^ _Cvolatilities;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phSwaptionVolatilityMatrix = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        optionTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIoptionTenors = optionTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCoptionTenors = safe_cast<CPeriodVector^>(_NCIoptionTenors);
        std::vector<QuantLib::Period>& _optionTenors = static_cast<std::vector<QuantLib::Period>&> (_NCoptionTenors->GetReference ());
        swapTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIswapTenors = swapTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCswapTenors = safe_cast<CPeriodVector^>(_NCIswapTenors);
        std::vector<QuantLib::Period>& _swapTenors = static_cast<std::vector<QuantLib::Period>&> (_NCswapTenors->GetReference ());
        _Cvolatilities = safe_cast<CMatrix^> (volatilities);
        _Cvolatilities->Lock();
        QuantLib::Matrix& _volatilities = static_cast<QuantLib::Matrix&> (_Cvolatilities->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (new QuantLib::SwaptionVolatilityMatrix ( _referenceDate,  _calendar,  _bdc,  _optionTenors,  _swapTenors,  _volatilities,  _dayCounter ));
        SetSwaptionVolatilityDiscrete (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (optionTenors != nullptr) optionTenors->Unlock();    //not optional
        if (swapTenors != nullptr) swapTenors->Unlock();    //not optional
        if (_Cvolatilities != nullptr) _Cvolatilities->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::QL::Math::IMatrix^ volatilities, Cephei::QL::Times::IDayCounter^ dayCounter) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
    CCalendar^ _Ccalendar;
    CMatrix^ _Cvolatilities;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phSwaptionVolatilityMatrix = NULL;
#endif
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        optionTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIoptionTenors = optionTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCoptionTenors = safe_cast<CPeriodVector^>(_NCIoptionTenors);
        std::vector<QuantLib::Period>& _optionTenors = static_cast<std::vector<QuantLib::Period>&> (_NCoptionTenors->GetReference ());
        swapTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIswapTenors = swapTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCswapTenors = safe_cast<CPeriodVector^>(_NCIswapTenors);
        std::vector<QuantLib::Period>& _swapTenors = static_cast<std::vector<QuantLib::Period>&> (_NCswapTenors->GetReference ());
        _Cvolatilities = safe_cast<CMatrix^> (volatilities);
        _Cvolatilities->Lock();
        QuantLib::Matrix& _volatilities = static_cast<QuantLib::Matrix&> (_Cvolatilities->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (new QuantLib::SwaptionVolatilityMatrix ( _calendar,  _bdc,  _optionTenors,  _swapTenors,  _volatilities,  _dayCounter ));
        SetSwaptionVolatilityDiscrete (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (optionTenors != nullptr) optionTenors->Unlock();    //not optional
        if (swapTenors != nullptr) swapTenors->Unlock();    //not optional
        if (_Cvolatilities != nullptr) _Cvolatilities->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (DateTime referenceDate, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::Core::IMatrix<Cephei::QL::IQuote^>^ vols, Cephei::QL::Times::IDayCounter^ dayCounter) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phSwaptionVolatilityMatrix = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        optionTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIoptionTenors = optionTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCoptionTenors = safe_cast<CPeriodVector^>(_NCIoptionTenors);
        std::vector<QuantLib::Period>& _optionTenors = static_cast<std::vector<QuantLib::Period>&> (_NCoptionTenors->GetReference ());
        swapTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIswapTenors = swapTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCswapTenors = safe_cast<CPeriodVector^>(_NCIswapTenors);
        std::vector<QuantLib::Period>& _swapTenors = static_cast<std::vector<QuantLib::Period>&> (_NCswapTenors->GetReference ());
        vols ->Lock ();
        INativeMatrix<Cephei::QL::IQuote^>^ _NCIvols = vols->getFeature (NativeFeature::Handle);
        CQuoteMatrix^ _NCvols = safe_cast<CQuoteMatrix^>(_NCIvols);
        std::vector<std::vector<Handle<QuantLib::Quote> > >& _vols = static_cast<std::vector<std::vector<Handle<QuantLib::Quote> > >&> (_NCvols->GetHandle ());
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (new QuantLib::SwaptionVolatilityMatrix ( _referenceDate,  _calendar,  _bdc,  _optionTenors,  _swapTenors,  _vols,  _dayCounter ));
        SetSwaptionVolatilityDiscrete (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (optionTenors != nullptr) optionTenors->Unlock();    //not optional
        if (swapTenors != nullptr) swapTenors->Unlock();    //not optional
        if (vols != nullptr) vols->Unlock();    //not optional
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::Core::IMatrix<Cephei::QL::IQuote^>^ vols, Cephei::QL::Times::IDayCounter^ dayCounter) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
    CCalendar^ _Ccalendar;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phSwaptionVolatilityMatrix = NULL;
#endif
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        optionTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIoptionTenors = optionTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCoptionTenors = safe_cast<CPeriodVector^>(_NCIoptionTenors);
        std::vector<QuantLib::Period>& _optionTenors = static_cast<std::vector<QuantLib::Period>&> (_NCoptionTenors->GetReference ());
        swapTenors ->Lock ();
        INativeVector<Cephei::QL::Times::IPeriod^>^ _NCIswapTenors = swapTenors->getFeature (NativeFeature::Value);
        CPeriodVector^ _NCswapTenors = safe_cast<CPeriodVector^>(_NCIswapTenors);
        std::vector<QuantLib::Period>& _swapTenors = static_cast<std::vector<QuantLib::Period>&> (_NCswapTenors->GetReference ());
        vols ->Lock ();
        INativeMatrix<Cephei::QL::IQuote^>^ _NCIvols = vols->getFeature (NativeFeature::Handle);
        CQuoteMatrix^ _NCvols = safe_cast<CQuoteMatrix^>(_NCIvols);
        std::vector<std::vector<Handle<QuantLib::Quote> > >& _vols = static_cast<std::vector<std::vector<Handle<QuantLib::Quote> > >&> (_NCvols->GetHandle ());
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (new QuantLib::SwaptionVolatilityMatrix ( _calendar,  _bdc,  _optionTenors,  _swapTenors,  _vols,  _dayCounter ));
        SetSwaptionVolatilityDiscrete (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (optionTenors != nullptr) optionTenors->Unlock();    //not optional
        if (swapTenors != nullptr) swapTenors->Unlock();    //not optional
        if (vols != nullptr) vols->Unlock();    //not optional
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix>& childNative, Object^ owner) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityMatrix = NULL;
#endif
	_ppSwaptionVolatilityMatrix = &childNative;
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (QuantLib::SwaptionVolatilityMatrix& childNative, Object^ owner) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityMatrix = NULL;
#endif
	_ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (&childNative);
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
    _SwaptionVolatilityMatrixOwner = owner;
    _SwaptionVolatilityDiscreteOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (CSwaptionVolatilityMatrix^ copy) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityMatrix = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (copy->GetShared());
        _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (PLATFORM::Type^ t) : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityMatrix = NULL;
#endif
	if (!t->IsSubclassOf(CSwaptionVolatilityMatrix::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (QuantLib::Handle<QuantLib::SwaptionVolatilityMatrix>& childNative, Object^ owner)  : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
	_phSwaptionVolatilityMatrix = &childNative;
	_ppSwaptionVolatilityMatrix = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix>>(childNative.currentLink());
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
    _SwaptionVolatilityMatrixOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (QuantLib::Handle<QuantLib::SwaptionVolatilityMatrix> childNative)  : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
	_phSwaptionVolatilityMatrix = &childNative;
	_ppSwaptionVolatilityMatrix = &static_cast<boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix>>(childNative.currentLink());
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::CSwaptionVolatilityMatrix (QuantLib::SwaptionVolatilityMatrix childNative)  : CSwaptionVolatilityDiscrete(CSwaptionVolatilityMatrix::typeid)
{
#ifdef HANDLE
	_phSwaptionVolatilityMatrix = NULL;
#endif
	_ppSwaptionVolatilityMatrix = new boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix> (new QuantLib::SwaptionVolatilityMatrix (childNative));
    _ppSwaptionVolatilityDiscrete = new boost::shared_ptr<QuantLib::SwaptionVolatilityDiscrete> (boost::dynamic_pointer_cast<QuantLib::SwaptionVolatilityDiscrete> (*_ppSwaptionVolatilityMatrix));
}
#endif

Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::~CSwaptionVolatilityMatrix ()
{
    if (_ppSwaptionVolatilityMatrix != NULL)
    {
	    delete _ppSwaptionVolatilityMatrix;
        _ppSwaptionVolatilityMatrix = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::!CSwaptionVolatilityMatrix ()
{
    if (_ppSwaptionVolatilityMatrix != NULL)
    {
	    delete _ppSwaptionVolatilityMatrix;
    }
}
QuantLib::SwaptionVolatilityMatrix& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::GetReference ()
{
    if (_ppSwaptionVolatilityMatrix == NULL) throw REFNEW NativeNullException ();
	return **_ppSwaptionVolatilityMatrix;
}
boost::shared_ptr<QuantLib::SwaptionVolatilityMatrix>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::GetShared ()
{
    if (_ppSwaptionVolatilityMatrix == NULL) throw REFNEW NativeNullException ();
	return *_ppSwaptionVolatilityMatrix;
}
QuantLib::SwaptionVolatilityMatrix* Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::GetPointer ()
{
    if (_ppSwaptionVolatilityMatrix == NULL) throw REFNEW NativeNullException ();
	return &**_ppSwaptionVolatilityMatrix;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SwaptionVolatilityMatrix>& Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::GetHandle ()
{
	if (_phSwaptionVolatilityMatrix == NULL)
	{
		_phSwaptionVolatilityMatrix = new Handle<QuantLib::SwaptionVolatilityMatrix> (*_ppSwaptionVolatilityMatrix);
	}
	return *_phSwaptionVolatilityMatrix;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::HasNative () 
{
	return (_ppSwaptionVolatilityMatrix != NULL);
}

DateTime Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSwaptionVolatilityMatrix)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSwaptionVolatilityMatrix)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::MaxSwapTenor::get ()
{
    try
    {
    	QuantLib::Period& _rv = (QuantLib::Period&)(*_ppSwaptionVolatilityMatrix)->maxSwapTenor ( );   
        Cephei::QL::Times::CPeriod^ _nrv = REFNEW Cephei::QL::Times::CPeriod (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSwaptionVolatilityMatrix)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix::PerformCalculations::get ()
{
    try
    {
    	(*_ppSwaptionVolatilityMatrix)->performCalculations ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix_Factory::Create (DateTime referenceDate, Cephei::Core::IVector<DateTime>^ optionDates, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::QL::Math::IMatrix^ volatilities, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CSwaptionVolatilityMatrix ( referenceDate,  optionDates,  swapTenors,  volatilities,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::QL::Math::IMatrix^ volatilities, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CSwaptionVolatilityMatrix ( referenceDate,  calendar,  bdc,  optionTenors,  swapTenors,  volatilities,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix_Factory::Create (Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::QL::Math::IMatrix^ volatilities, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CSwaptionVolatilityMatrix ( calendar,  bdc,  optionTenors,  swapTenors,  volatilities,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::Core::IMatrix<Cephei::QL::IQuote^>^ vols, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CSwaptionVolatilityMatrix ( referenceDate,  calendar,  bdc,  optionTenors,  swapTenors,  vols,  dayCounter);
}
Cephei::QL::Termstructures::Volatility::Swaption::ISwaptionVolatilityMatrix^ Cephei::QL::Termstructures::Volatility::Swaption::CSwaptionVolatilityMatrix_Factory::Create (Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ optionTenors, Cephei::Core::IVector<Cephei::QL::Times::IPeriod^>^ swapTenors, Cephei::Core::IMatrix<Cephei::QL::IQuote^>^ vols, Cephei::QL::Times::IDayCounter^ dayCounter)
{
    return REFNEW CSwaptionVolatilityMatrix ( calendar,  bdc,  optionTenors,  swapTenors,  vols,  dayCounter);
}
